#include "Gamemap.h"
#include<QFile>
#include<QDebug>

Gamemap::Gamemap(QObject *parent) : QObject(parent)
{
    mRow=0;
    mCol=0;
    mPArr=nullptr;
}
Gamemap::~Gamemap()
{
    Clear();
}
void Gamemap::Clear()
{
    if(mPArr != nullptr)
    {
        for(int i=0;i<mRow;i++)
        {
            delete[] mPArr[i];
        }
       delete[] mPArr;
    }

}
 bool Gamemap::InitByFile(QString fileName)
{
     QFile file(fileName);//创建对象
    if(! file.open(QIODevice::ReadOnly))
     {
         return false;//如果打开失败，返回0
     }

    //读取所有内容
    QByteArray arrAll= file.readAll();
    arrAll.replace("\r\n","\n");
    QList<QByteArray> lineList =arrAll.split('\n');
    mRow=lineList.size();//确定行
    mPArr= new int*[mRow];

    for(int i=0;i<mRow;i++)
    {
        QList<QByteArray> colList =lineList[i].split(',');
        mCol = colList.size();//确定列
        mPArr[i]= new int[mCol];
        for(int j=0;j<mCol;j++)
        {
            mPArr[i][j] = colList[j].toInt();
        }
        
    }


}
void Gamemap::Paint(QPainter*_p,QPoint _Pos)
{
    for(int i=0;i<mRow;i++)
    {
        for(int j=0;j<mCol;j++)
        {
            QString imgUrl;
            switch(mPArr[i][j])
            {
                case Road: imgUrl = "://imag/road.png";break;
                case Wall:imgUrl = "://imag/wall.png"; break;
                case Box:imgUrl = "://imag/box.png"; break;
                case Point:imgUrl = "://imag/point.png"; break;
                case InPoint: imgUrl = "://imag/inpoint.png";break;
            }
            QImage img(imgUrl);
            _p->drawImage(QRect(_Pos.x()+j*64,_Pos.y()+i*64,64,64),img);

        }
    }
}
